package com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leaveRecord.servlet;

import com.alibaba.fastjson.JSON;
import com.yunzheng.studyLeaveSystem.modules.response.ResponseWrapper;
import com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leave.entity.Leave;
import com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leave.service.LeaveService;
import com.yunzheng.studyLeaveSystem.modules.user.leaveFunction.leaveRecord.service.LeaveRecordService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "LeaveRecordServlet" , value = "/LeaveRecordServlet")
public class LeaveRecordServlet extends HttpServlet {
    LeaveService leaveService = new LeaveService();
    LeaveRecordService leaveRecordService = new LeaveRecordService();

    /**
     * 通过登陆时填写的手机号码，session请求到查询请假记录页面
     * @param req 请求
     * @param resp 响应
     * @throws IOException 抛出异常
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.setAttribute("id",7);
        String ID = session.getAttribute("id").toString();
        int id = Integer.parseInt(ID);
        List<Leave> records = leaveRecordService.record(id);
        System.out.println(id);
        ResponseWrapper responseWrapper = null;
        if (records == null || records.size() == 0){
            responseWrapper = ResponseWrapper.SuccessButNoData();
        }else {
            responseWrapper = ResponseWrapper.Success(records);
        }
        String jsonString = JSON.toJSONString(responseWrapper);
        resp.getWriter().print(jsonString);
        System.out.println("已返回状态码：" + jsonString);
    }

    /**
     * 调用了doGet方法
     * @param req 请求
     * @param resp 响应
     * @throws ServletException 抛出异常
     * @throws IOException 抛出异常
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}